;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?merchantTypeId=") != -1) {
			var reqPath = basePath+'merchantTypeManager/queryMerchantTypeById.action'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var merchanttype = res.data;
						$("#uu_id").val(merchanttype.uuid);
						$("#type_name").val(merchanttype.typeName);
						$("#type_strategy").val(merchanttype.typeStrategy);
						if(merchanttype.typeStrategy=='2')
						{
							$('#merchant_img').hide();
						}
						if(null!=merchanttype.listAdInfo)
						{
							var adInfo = '';
							$.each(merchanttype.listAdInfo, function(i,n)
									{
								adInfo += '<input type="checkbox" name="bindToId" value="'+n.uuid+'" checked="checked"/>'+n.businessName+' ';
									});
							$("#ad_info").append(adInfo);
						}
						if(null!=merchanttype.listUnAdInfo)
						{
							var unAdInfo = '';
							$.each(merchanttype.listUnAdInfo, function(i,n)
									{
								unAdInfo += '<input type="checkbox" name="bindToId" value="'+n.uuid+'" />'+n.businessName+' ';
									});
							$("#un_ad_info").append(unAdInfo);
						}
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else if (url.indexOf("?type=") != -1)
		{
			$('#parent_type').hide();
			$('#merchant_type').bind('click', function()
			{
				if($('#merchant_type').val()=='1')
				{
					$('#parent_type').hide();
					$('#merchant_img').show();
				}else if ($('#merchant_type').val()=='2')
				{
					$('#merchant_img').hide();
					$('#parent_type').show();
				}
			});
			queryParentType();
			queryAdList();
		}else
		{
			//进页面默认加载列表
			initData(0,15);
		}
		$('#addnew').click(function()
		{
			window.location.href = jumpPath + 'view/technology/merchanttype/addmerchanttype.jsp?type=addmerchanttype';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			initData(0,15);
		});

		$('#tbn_save').bind('click', function()
		{
			savemerchanttype();
//			window.location = jumpPath + 'view/technology/merchanttype/merchanttypelist.jsp';
		});
		
		$('#tbn_update').bind('click', function()
		{
			updatemerchanttype();
		});
		
	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function editmerchanttype(merchanttypeId)
{
	window.location.href = jumpPath + 'view/technology/merchanttype/editmerchanttype.jsp?merchantTypeId='+merchanttypeId;
}

function del(id)
{

	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'JobWanted/deleteJobWanted.action?JobWantedId='+id;
		send(reqPath, "POST", "application/json", "json", null, function(res)
		{
			if (res.result == 'success')
			{
				initData(0,15);
			}
		});
	}
}

function initData(pageNumber,pagesize)
{

	var reqPath = basePath+'JobWanted/queryJobWantedByPage.action';
	var sendBody = '{"currentPage": 1,"pageSize": 15}';
	var $tab = $('#adinfoDataTables tbody').html("");

	
	var reqPath = basePath + 'JobWanted/queryJobWantedByPage.action';
	var sendBody = '{"currentPage": '+pageNumber+',"pageSize": '+pagesize+'}';
	var $tab = $('#jobwantedDataTables tbody').html("");

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';
				$.each(res.data.data, function(i,n)
				{
					var operation = "<button onclick='del(\""+n.uuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					if(n.isClose == 'N')
					{
						operation += " <button onclick='closeJobwanted(\""+n.uuid+"\")' type='button' class='btn btn-primary'>关闭</button>";
					}else if (n.isClose == 'Y')
					{
						operation += " <button onclick='openJobwanted(\""+n.uuid+"\")' type='button' class='btn btn-primary'>打开</button>";
					}
					rows += tpl_tr.replace(/\$userKey/g, n.userName)
					.replace(/\$education/,n.education)
					.replace(/\$specialty/, n.specialty)
					.replace(/\$graduationTime/, n.graduationTime)
					.replace(/\$workExperience/, n.workExperience)
					.replace(/\$intention/, n.intention)
					.replace(/\$phone/, n.phone)
					.replace(/\$qq/, n.qq)
					.replace(/\$operation/, operation);
				});
				rows = rows.replace(/null/gm,"");
				$tab.append(rows);
				// page
				pagefunction(initData,res.data.totalPages,pagesize);
			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

function closeJobwanted(id)
{
	var requsetPath = basePath + "JobWanted/updateJobWanted.action";
	var sendBody = '{"uuid": "'+id+'","isClose": "Y"}';
	send(requsetPath, "POST", "application/json", "json", sendBody,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/jobwanted/jobwantedlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function openJobwanted(id)
{
	var requsetPath = basePath + "JobWanted/updateJobWanted.action";
	var sendBody = '{"uuid": "'+id+'","isClose": "N"}';
	send(requsetPath, "POST", "application/json", "json", sendBody,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/jobwanted/jobwantedlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

/**
 * 
 * @METHODNAME: saveLoginadinfo
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function savemerchanttype()
{
 	$('#frm_merchanttype').submit();
}
function closeMerchantType(id)
{
	var requsetPath = basePath + "merchantTypeManager/updateMerchantType.action";
	var sendBody = '{"uuid": "'+id+'","isClose": "Y"}';
	send(requsetPath, "POST", "application/json", "json", sendBody,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/merchanttype/merchanttypelist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function openMerchantType(id)
{
	var requsetPath = basePath + "merchantTypeManager/updateMerchantType.action";
	var sendBody = '{"uuid": "'+id+'","isClose": "N"}';
	send(requsetPath, "POST", "application/json", "json", sendBody,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/merchanttype/merchanttypelist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function updatemerchanttype()
{
	debugger;
	var bindToId = '';
	$("input:[name='bindToId']:checked").each(function() {
		bindToId +=$(this).val()+",";
	});
	var requsetPath = basePath + "merchantTypeManager/updateMerchantType.action";
	var requestData = $('#frm_merchanttype').serializeObject();
	$("#bindTo_id").val(bindToId.substr(0,bindToId.length-1));
	$('#frm_merchanttype').submit();
//	requestData.bindToId = bindToId.substr(0,bindToId.length-1);
//	requestData = JSON.stringify(requestData);
//	console.log(requestData);
//	send(requsetPath, "POST", "application/json", "json", requestData,
//			function(resultData)
//			{
//
//				if (resultData.result == 'success')
//				{
//					// 跳转到列表
//					window.location = jumpPath
//							+ 'view/technology/merchanttype/merchanttypelist.jsp';
//				} else
//				{
//					alert(resultData.errorMsg);
//				}
//			});
}

function queryParentType()
{
	var requsetPath = basePath + "merchantTypeManager/queryParentMerchantType.action";
	send(requsetPath, "POST", "application/json", "json", "{}",
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					$.each(resultData.data, function(i,n)
					{
						$("#parent_id").append("<option value='"+n.uuid+"'>"+n.typeName+"</option>");
					});
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function queryAdList()
{
	debugger;
	var requsetPath = basePath + "adManager/queryAdList.action";
	send(requsetPath, "POST", "application/json", "json", "{}",
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					var adInfo = '';
					$.each(resultData.data, function(i,n)
					{
						adInfo += '<input type="checkbox" name="bindToId" value="'+n.uuid+'"/>'+n.businessName+' '
					});
					$("#ad_info").append(adInfo);
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}
